package com.liu.cmsService.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.R;
import com.liu.cmsService.entity.CrmBanner;
import com.liu.cmsService.service.CrmBannerService;
import com.liu.serviceBase.exception.MyException;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 后端admin控制器
 * </p>
 *
 * @author LSJ
 * @since 2022-01-17
 */
@RestController
@RequestMapping("/cmsService/crmbanner")
@CrossOrigin
public class CrmBannerController {

    @Autowired
    private CrmBannerService crmBannerService;

    //分页查询
    @PostMapping("pagesQuery/{page}/{limit}")
    public R pagesQuery(
        @ApiParam(name = "page",value = "页数",required = true)
        @PathVariable   Long page,
        @ApiParam(name = "limit",value = "显示条数",required = true)
        @PathVariable   Long limit,
        @ApiParam(name = "title",value = "标题",required = false)
        @RequestParam(required = false)   String title){

        Page<CrmBanner> bannerPage = new Page<>(page,limit);

        crmBannerService.pagesQuery(bannerPage,title);
        long total = bannerPage.getTotal();

        List<CrmBanner> bannerList = bannerPage.getRecords();

        return R.ok().data("total",total).data("bannerList",bannerList);

    }

//    由id获取轮播图
    @GetMapping("getBannerById/{id}")
    public R getBannerById(
            @ApiParam(name = "id" ,value = "轮播图id")
            @PathVariable String id){
        if (ObjectUtils.isEmpty(id)){
            throw new MyException(20001,"getBannerById 的ID为空");
        }
        CrmBanner banner = crmBannerService.getById(id);

        return R.ok().data("banner",banner);
    }

//    添加轮播图
    @PostMapping("saveBanner")
    public R saveBanner ( @RequestBody  CrmBanner banner){
        boolean save = crmBannerService.save(banner);
        return save?R.ok():R.error();
    }

    //    删除轮播图
    @DeleteMapping("{id}")
    public R removeBanner ( @PathVariable String id){
        boolean remove = crmBannerService.removeById(id);
        return remove?R.ok():R.error();
    }

    //    修改轮播图
    @PostMapping("updateBanner")
    public R updateBanner ( @RequestBody  CrmBanner banner){
        boolean update = crmBannerService.updateById(banner);
        return update?R.ok():R.error();
    }


}

